<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="标题"><u-input v-model="perform.title" type="text" placeholder="请输入标题" /></u-form-item>
					<u-form-item class="form-item" label="届次">
						<u-input v-model="jieci" type="select" :select-open="jcShow" placeholder="请选择届次" @click="jcShow = true" />
						<u-select v-model="jcShow" mode="mutil-column-auto" :list="jcList" @confirm="jcConfirm"></u-select>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<view class="search-view"><u-search class="search-input" shape="square" bg-color="#fff" v-model="perform.title" :show-filter="true" @search="search" @custom="show = !show"></u-search></view>
		<u-tabs class="uTabs" :list="tabsList" :is-scroll="false" :current="tabsCurrent" @change="tabsChange" :key="tabsList.length"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_01" v-for="(item, index) of dutiesList" :key="item.id" @click="navTo(item)">
				<view class="info-title">{{ item.title }}</view>
				<view class="info-btm">
					<view class="btm-left clamp">
						<text class="left-name">{{ item.createByName }}</text>
						<text class="left-time">{{ item.performDate }}</text>
					</view>
					<view class="btm-right">
						<view class="right-type" :style="'color:' + colorMap[item.nodeStatus]">{{ statusMap[item.nodeStatus] }}</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="loading" />
		</view>
		<image src="/static/img/icon_tianbao.png" mode="widthFix" class="tianbao" @click="editTo" v-if="$sysBtns(flag + '_add')"></image>
		<u-back-top :scroll-top="scrollTop" top="200" style="right: 40rpx; bottom: 50rpx"></u-back-top>

		<view v-if="false && flag"></view>
	</view>
</template>

<script>
import { mapState } from 'vuex';
export default {
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabsCurrent: 0,
			tabsList: [],
			colorMap: {
				0: '#303133',
				1: '#19be6b',
				2: '#ff9900',
				3: '#0099ff',
				4: '#ff3333'
			},
			statusMap: {
				0: '草稿',
				1: '待初审',
				2: '待审核',
				3: '通过',
				4: '不通过'
			},
			nodeStatus: -1,
			title: '人大会议',
			currentUserId: '',
			jieci: '',
			jcShow: false,
			jcList: [],
			flag: 'huiyi',
			dutiesList: [],

			perform: {
				ciId: '',
				jieId: '',
				status: '1',
				wyhdz: '',
				nodeStatus: '',
				module: '1',
				type: '',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.code) {
			this.flag = option.flag;
			this.perform.module = option.code;
			this.title = option.name;
		}
		if (option && option.wyhdz) {
			this.perform.wyhdz = option.wyhdz;
		}
		if (option && option.type) {
			this.perform.type = option.type;
		}
		if (option && option.jieId) {
			this.perform.jieId = option.jieId;
		}
		if (option && option.ciId) {
			this.perform.ciId = option.ciId;
		}
		if (option && option.currentUserId) {
			this.currentUserId = option.currentUserId;
		}
		uni.setNavigationBarTitle({
			title: this.title
		});
		this.loadData();
		this.jieciData();
		this.loadDict('perform_status');
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.roleCodes = this.userInfo.roleCodes.join(',');

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			if (this.perform.nodeStatus == '-1') {
				this.perform.nodeStatus = '';
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				clientCode: this.clientCode,
				roleCodes: this.roleCodes,
				perform: this.perform
			};
			this.$request('/perform/myList', data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.dutiesList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.dutiesList = this.dutiesList.concat(res.data.records);
				this.loaded = true;
				if (this.dutiesList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadDict(type) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				this.tabsList.push({
					name: '所有',
					value: '-1'
				});
				res.data.forEach((item) => {
					this.tabsList.push({
						name: item.dictName,
						value: item.dictValue
					});
				});
			});
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.type(this.tabsList[index].value);
		},
		jcConfirm(e) {
			this.jieci = e[0].label + e[1].label;
			this.perform.jieId = e[0].value;
			this.perform.ciId = e[1].value;
		},
		filter() {
			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.perform.title = '';
			this.jieci = '';
			this.perform.jieId = '';
			this.perform.ciId = '';
			this.loadData('refresh');
		},
		search(e) {
			this.perform.title = e;
			this.loadData('refresh');
		},
		type(e) {
			this.nodeStatus = e;
			this.perform.nodeStatus = e.toString();
			this.loadData('refresh');
		},
		jieciData() {
			let data = {
				clientCode: this.clientCode,
				jieCi: {
					pid: ''
				}
			};
			this.$request('/jie-ci/list', data).then((res) => {
				this.jcList = [];
				let jieArr = [];
				let ciArr = [];
				res.data.forEach((item) => {
					data.jieCi.pid = item.id;
					this.$request('/jie-ci/list', data).then((res) => {
						ciArr = [];
						res.data.forEach((key) => {
							ciArr.push({
								value: key.id,
								label: key.name
							});
						});
						this.jcList.push({
							value: item.id,
							label: item.name,
							children: ciArr
						});
					});
				});
			});
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pagesA/duties/detail?flag=' + this.flag + '&code=' + this.perform.module + '&name=' + this.title + '&id=' + item.id
			});
		},
		editTo(item) {
			uni.navigateTo({
				url: '/pagesA/duties/add?flag=' + this.flag + '&code=' + this.perform.module + '&name=' + this.title
			});
		}
	}
};
</script>

<style lang="scss">
.uTabs {
	min-height: 88rpx;
}

.list-box {
	padding: 0 30rpx;
	background-color: #fff;
}

.tianbao {
	display: block;
	position: fixed;
	right: 40rpx;
	bottom: 270rpx;
	width: 160rpx;
	height: 160rpx;
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
